zz_

src/isPerfectSquare.js

zzmq.dev@gmail.com[dev2]/mathkit[add-lcm-b-function-returns-least-common] / src / isPerfectSquare.js @ d0011d30 Download
Ladder Summary
  • file Implements isPerfectSquare, a function that validates its argument is a finite, integer, non‑negative number and then determines if it is a perfect square by checking whether the square root is an integer, returning true only for valid perfect squares.
/**
 * Determines whether a given number is a perfect square.
 *
 * @param {number} n - The number to test.
 * @returns {boolean} True if n is a non‑negative integer that is a perfect square, otherwise false.
 */
export function isPerfectSquare(n) {
  if (typeof n !== "number" || !Number.isFinite(n)) return false;
  if (!Number.isInteger(n)) return false;
  if (n < 0) return false;

  const root = Math.sqrt(n);
  return Number.isInteger(root);
}